//
//  RoyAutoresizingTextView.m
//  StickerDemo
//
//  Created by 王若聪 on 2016/12/13.
//  Copyright © 2016年 ckj. All rights reserved.
//

#import "RoyAutoresizingTextView.h"

#define kStickerMinScale 0.5f
#define kStickerMaxScale 5.0f

@interface RoyAutoresizingTextView ()<UIGestureRecognizerDelegate>
@property (nonatomic,assign) CGFloat keyboardHeight;
@end

@implementation RoyAutoresizingTextView

- (instancetype)initWithType:(RoyAutoresizingTextViewType)type{
    self = [super init];
    
    if (self) {
        
        self.exclusiveTouch = YES;
        self.userInteractionEnabled = YES;
        [self attachGestures];

        //增加监听，当键盘出现或改变时收出消息
        [[NSNotificationCenter defaultCenter] addObserver:self
                                                 selector:@selector(keyboardWillShow:)
                                                     name:UIKeyboardWillShowNotification
                                                   object:nil];
        
        //增加监听，当键退出时收出消息
        [[NSNotificationCenter defaultCenter] addObserver:self
                                                 selector:@selector(keyboardWillHide:)
                                                     name:UIKeyboardWillHideNotification
                                                   object:nil];
        
        self.type = type;
        
        switch (self.type) {
            case RoyAutoresizingTextViewTypeVertical:
                self.frame = CGRectMake(0, 300, [UIScreen mainScreen].bounds.size.width, 40);
                self.backgroundColor = [UIColor colorWithRed:65/255.0 green:65/255.0 blue:65/255.0 alpha:0.5];
                self.text = @"";
                self.textColor = [UIColor whiteColor];
                self.textAlignment = NSTextAlignmentLeft;
                self.scrollEnabled = NO;
                break;
            case RoyAutoresizingTextViewTypeFreedomTextLeft:
                self.frame = CGRectMake(0, 300, [UIScreen mainScreen].bounds.size.width, 80);
                self.backgroundColor = [UIColor clearColor];
                self.text = @"";
                self.textColor = [UIColor whiteColor];
                self.textAlignment = NSTextAlignmentLeft;
                self.scrollEnabled = NO;
                break;
            case RoyAutoresizingTextViewTypeFreedomTextCenter:
                self.frame = CGRectMake(0, 300, [UIScreen mainScreen].bounds.size.width, 80);
                self.backgroundColor = [UIColor clearColor];
                self.text = @"";
                self.textColor = [UIColor whiteColor];
                self.textAlignment = NSTextAlignmentCenter;
                self.scrollEnabled = NO;
                break;
            default:
                
                break;
        }
        
        
    }
    
    return self;
    
}

- (void)drawRect:(CGRect)rect{
    [super drawRect:rect];
    
    switch (self.type) {
        case RoyAutoresizingTextViewTypeVertical:{
            
            self.type = RoyAutoresizingTextViewTypeFreedomTextLeft;
            self.frame = CGRectMake(0, [UIScreen mainScreen].bounds.size.height - self.keyboardHeight -40, [UIScreen mainScreen].bounds.size.width, 40);
            self.font = [UIFont systemFontOfSize:20];

            self.backgroundColor = [UIColor clearColor];

            self.textAlignment = NSTextAlignmentLeft;
        }
            break;
        case RoyAutoresizingTextViewTypeFreedomTextLeft:{
            
            self.type = RoyAutoresizingTextViewTypeFreedomTextCenter;

            self.frame = CGRectMake(0, 300, [UIScreen mainScreen].bounds.size.width, 100);
            
            self.font = [UIFont systemFontOfSize:30];
            
            self.backgroundColor = [UIColor clearColor];

            self.textAlignment = NSTextAlignmentLeft;
        }
            break;
        case RoyAutoresizingTextViewTypeFreedomTextCenter:{
            self.type = RoyAutoresizingTextViewTypeVertical;

            self.frame = CGRectMake(0, 300, [UIScreen mainScreen].bounds.size.width, 100);
            self.font = [UIFont systemFontOfSize:30];
            self.backgroundColor = [UIColor colorWithRed:65/255.0 green:65/255.0 blue:65/255.0 alpha:0.5];

            self.textAlignment = NSTextAlignmentCenter;

        }
            break;
        default:
            
            break;
    }

}




- (void)attachGestures {
    // ContentView
    //图片交互的手势
    UIRotationGestureRecognizer *rotateGesture = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(handleRotate:)];
    [rotateGesture setDelegate:self];
    [self addGestureRecognizer:rotateGesture];
    
    UIPinchGestureRecognizer *pinGesture = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(handleScale:)];
    [pinGesture setDelegate:self];
    [self addGestureRecognizer:pinGesture];
    
    UIPanGestureRecognizer *panGesture = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(handleMove:)];
    [panGesture setMinimumNumberOfTouches:1];
    [panGesture setMaximumNumberOfTouches:2];
    [panGesture setDelegate:self];
    [self addGestureRecognizer:panGesture];
    
//    UITapGestureRecognizer *tapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTap:)];
//    [tapRecognizer setNumberOfTapsRequired:1];
//    [tapRecognizer setDelegate:self];
//    [self addGestureRecognizer:tapRecognizer];
    
}



//以下三个为贴纸的移动，放大,旋转手势
- (void)handleMove:(UIPanGestureRecognizer *)gesture {
    CGPoint translation = [gesture translationInView:[self superview]];
    CGPoint targetPoint = CGPointMake(self.center.x + translation.x, self.center.y + translation.y);

    
    
    switch (self.type) {
        case RoyAutoresizingTextViewTypeVertical:{
            //上一次是RoyAutoresizingTextViewTypeFreedomTextCenter
            
            //上下左右都可以移动的类型
            // Boundary detection
            targetPoint.x = MAX(0, targetPoint.x);
            targetPoint.y = MAX(0, targetPoint.y);
            targetPoint.x = MIN(self.superview.bounds.size.width, targetPoint.x);
            targetPoint.y = MIN(self.superview.bounds.size.height, targetPoint.y);
            

        }
            break;
        case RoyAutoresizingTextViewTypeFreedomTextLeft:{
            //上一次是RoyAutoresizingTextViewTypeVertical

            //像snapchat那样只能上下移动，而不能左右移动
            targetPoint.x = MAX(0, self.center.x);
            targetPoint.x = MIN(self.superview.bounds.size.width, self.center.x);
            targetPoint.y = MAX(0, targetPoint.y);
            targetPoint.y = MIN(self.superview.bounds.size.height, targetPoint.y);
        }
            break;
        case RoyAutoresizingTextViewTypeFreedomTextCenter:{
            //上一次是RoyAutoresizingTextViewTypeFreedomTextLeft

            //上下左右都可以移动的类型
            // Boundary detection
            targetPoint.x = MAX(0, targetPoint.x);
            targetPoint.y = MAX(0, targetPoint.y);
            targetPoint.x = MIN(self.superview.bounds.size.width, targetPoint.x);
            targetPoint.y = MIN(self.superview.bounds.size.height, targetPoint.y);
        }
            break;
        default:
            
            break;
    }
    
//    if (self.type == RoyAutoresizingTextViewTypeVertical) {
//        //像snapchat那样只能上下移动，而不能左右移动
//        targetPoint.x = MAX(0, self.center.x);
//        targetPoint.x = MIN(self.superview.bounds.size.width, self.center.x);
//        targetPoint.y = MAX(0, targetPoint.y);
//        targetPoint.y = MIN(self.superview.bounds.size.height, targetPoint.y);
//    }else {
//        //上下左右都可以移动的类型
//        // Boundary detection
//        targetPoint.x = MAX(0, targetPoint.x);
//        targetPoint.y = MAX(0, targetPoint.y);
//        targetPoint.x = MIN(self.superview.bounds.size.width, targetPoint.x);
//        targetPoint.y = MIN(self.superview.bounds.size.height, targetPoint.y);
//    }
    
    
    [self setCenter:targetPoint];
    [gesture setTranslation:CGPointZero inView:[self superview]];
}

- (void)handleScale:(UIPinchGestureRecognizer *)gesture {
    CGFloat scale = gesture.scale;
    // Scale limit
    CGFloat currentScale = [[self.layer valueForKeyPath:@"transform.scale"] floatValue];
    if (scale * currentScale <= kStickerMinScale) {
        scale = kStickerMinScale / currentScale;
    } else if (scale * currentScale >= kStickerMaxScale) {
        scale = kStickerMaxScale / currentScale;
    }
    
    self.transform = CGAffineTransformScale(self.transform, scale, scale);
    gesture.scale = 1;
    

}

- (void)handleRotate:(UIRotationGestureRecognizer *)gesture {
    self.transform = CGAffineTransformRotate(self.transform, gesture.rotation);
    gesture.rotation = 0;
    
}


//当键盘出现或改变时调用
- (void)keyboardWillShow:(NSNotification *)aNotification
{
    //获取键盘的高度
    NSDictionary *userInfo = [aNotification userInfo];
    NSValue *aValue = [userInfo objectForKey:UIKeyboardFrameEndUserInfoKey];
    CGRect keyboardRect = [aValue CGRectValue];
    
    self.keyboardHeight = keyboardRect.size.height;
    
    self.frame = CGRectMake(0, [UIScreen mainScreen].bounds.size.height - self.keyboardHeight -40, [UIScreen mainScreen].bounds.size.width, 40);
}

//当键退出时调用
- (void)keyboardWillHide:(NSNotification *)aNotification{}

//- (void)handleTap:(UITapGestureRecognizer *)gesture {
//    if (gesture.view == self) {
//        
//        //点击选中某个贴纸
//        if (_autoresizingTextViewDelegate && [_autoresizingTextViewDelegate respondsToSelector:@selector(autoresizingTextViewDidTap:)]) {
//            [_autoresizingTextViewDelegate autoresizingTextViewDidTap:self];
//        }
//        
//        
//    }
//}

#pragma mark - UIGestureRecognizerDelegate

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer {
    if ([gestureRecognizer isKindOfClass:[UITapGestureRecognizer class]] || [otherGestureRecognizer isKindOfClass:[UITapGestureRecognizer class]]) {
        return NO;
    } else {
        return YES;
    }
}


@end
